﻿#region Using Statements
using System;
using System.Diagnostics;
using GameBase.Screens;
using Microsoft.Xna.Framework;
using StarFox.Inputs;
using StarFox.Sounds;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Basic properties needed to implement the player starship.
    /// </summary>
    public partial class StarShip : ScreenComponent3D, IDisposable
    {
        #region Fields

        IShipInput input;
        GamePlaySound sound;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent3D is associated to.</param>
        public StarShip(Screen screen)
            : base(screen)
        {
            input = Game.Services.GetService(typeof(IShipInput)) as IShipInput;
            if (input == null)
                throw new InvalidOperationException("Service of type IShipInput not found");

            sound = Game.Services.GetService(typeof(GamePlaySound)) as GamePlaySound;
            if (sound == null)
                throw new InvalidOperationException("Service of type StarFoxSound not found");

            Initialize_Physics();
            Initialize_Player();
        }

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Immediately releases all resources used by this object.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
        /// </summary>
        ~StarShip()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    sound.Motor_Stop();
                }

                isDisposed = true;
            }
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent3D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            Update_Player(gameTime);
            Update_Physics(gameTime);
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent3D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
        }

        #endregion
    }
}
